package com.example.tool.ui;

import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.jetbrains.annotations.Nullable;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * <p>Title: FormDialogWrapper</p >
 * Description: 更新资源文件
 * <p>CreateTime: 2022/6/29 14:32</p>
 * @author FT
 */
public class UpdateExeDialogWrapper extends DialogWrapper {
  private final Project project;
  private TextFieldWithBrowseButton userPasswordExe;
  private TextFieldWithBrowseButton creatFormExe;

  public TextFieldWithBrowseButton getUserPasswordExe() {
    return userPasswordExe;
  }

  public TextFieldWithBrowseButton getCreatFormExe() {
    return creatFormExe;
  }

  public UpdateExeDialogWrapper(Project project) {
    super(true);
    this.project = project;
    setTitle("Config Information");
    setSize(550, 260);
    setResizable(false);
    init();
  }


  @Override
  public @Nullable JComponent getPreferredFocusedComponent() {
    return userPasswordExe;
  }

  @Override
  protected @Nullable JComponent createCenterPanel() {
    userPasswordExe = new TextFieldWithBrowseButton();
    FileChooserDescriptor userPasswordFile = new FileChooserDescriptor(true, false, false, false, false, false);
    userPasswordExe.addBrowseFolderListener(new TextBrowseFolderListener(userPasswordFile));

    creatFormExe = new TextFieldWithBrowseButton();
    FileChooserDescriptor creatFormFile = new FileChooserDescriptor(true, false, false, false, false, false);
    creatFormExe.addBrowseFolderListener(new TextBrowseFolderListener(creatFormFile));

    JPanel jPanel = new JPanel();
    jPanel.removeAll();
    jPanel.setLayout(new GridLayoutManager(5, 2));
    SwingUtil.addChild(jPanel, new JLabel("User Password Exe: "), 0, 0);
    SwingUtil.addChild(jPanel, userPasswordExe, 0, 1, true);
    SwingUtil.addChild(jPanel, new JLabel("Creat Form Exe: "), 1, 0);
    SwingUtil.addChild(jPanel, creatFormExe, 1, 1, true);
    SwingUtil.addChild(jPanel, new JLabel(), 2);
    SwingUtil.addChild(jPanel, new JLabel("注意删除掉用户目录下的文件时若要使用新的文件需要重新更新文件。"), 3);
    SwingUtil.addChild(jPanel, new Spacer(), 4, 0, true);
    return jPanel;
  }

  @Override
  protected void doOKAction() {
    String userPasswordExeText = userPasswordExe.getText();
    String creatFormExeText = creatFormExe.getText();
    try {
      if (!userPasswordExeText.equals("") && !userPasswordExeText.substring(userPasswordExeText.lastIndexOf(".") + 1).equals("exe")) {
        throw new Exception();
      }
      if (!creatFormExeText.equals("") && !creatFormExeText.substring(creatFormExeText.lastIndexOf(".") + 1).equals("exe")) {
        throw new Exception();
      }
      super.doOKAction();
    } catch (Exception e) {
      Messages.showMessageDialog(project, "Please select Exe file!", "Error", HToolIcon.ModuleIcon);
    }
  }
}
